
// Created by codex on 2024/7/9.
//
//create by codex in 2024/3/25
#include "Peripheral.h"
#include "Drv_Screen.h"
#include "usart.h"
#include <stdio.h>
#include <stdarg.h>
#include <string.h>

uint8_t Screen_Receive;
uint8_t Screen_height=100;
uint8_t Screen_data_t[50]={0};
uint8_t data_Screen_t[50]={0};
float Detected_Temp=0;
uint8_t Screen_DataAnl(uint8_t *data_t)
{

    if(!(data_t[0] == 0x3f && data_t[2] == 0xff && data_t[3] == 0xff))
        return 0xff;

    Screen_data_t[0]=0xAA;
    Screen_data_t[1]=0xE2;
    Screen_data_t[2]=0x10;
    Screen_data_t[3]=1;
    Screen_data_t[4]=data_t[1];
    uint check_sum1 = 0, check_sum2 = 0;
    for (uint8_t i = 0; i < Screen_data_t[3] + 4; i++) {
        check_sum1 += Screen_data_t[i];
        check_sum2 += check_sum1;
    }
    Screen_data_t[Screen_data_t[3] + 4] = check_sum1;
    Screen_data_t[Screen_data_t[3] + 5] = check_sum2;

//    Debug_log("get screen data:%x %x %x %x", data_t[0], data_t[1], data_t[2], data_t[3]);

    return 7;
}

void Transmit_upData2SCreen(uint8_t *data) //转发主控数据到串口屏
{
    static int err_cnt = 0, rec_cnt = 0;
    if(rec_cnt == 10000)
    {
        rec_cnt = 0;
        err_cnt = 0;
    }
    rec_cnt++;
    int len=data[3]+6; //数据帧总长 data[3]实际数据长
    uint8_t check_sum1 = 0, check_sum2 = 0;
    for (uint8_t i = 0; i < len - 2; i++)
    {
        check_sum1 += *(data + i);
        check_sum2 += check_sum1;
    }
    if ((check_sum1 != *(data + len - 2)) || (check_sum2 != *(data + len - 1))) //ÅÐ¶ÏsumÐ£Ñé
    {
        err_cnt++;
        return;
    }
    if(*(data + 1) != SCREEN_TYPE)
    {
        err_cnt++;
        return;
    }

    union uint8_2_s32 _scr_temp;
    _scr_temp._u8_temp[0] = 0xFF-*(data + 4);
    _scr_temp._u8_temp[1] = 0xFF-*(data + 5);
    _scr_temp._u8_temp[2] = 0xFF-*(data + 6);
    _scr_temp._u8_temp[3] = 0xFF-*(data + 7);
//    Debug_log("get screen data: %d->%d", *(data + 2) - 2, _scr_temp._s32_temp);
//    Debug_log("err:%d.", (int)(100.0 * err_cnt / rec_cnt));

    switch (*(data + 2) - 2) {
        case 0: //x
        {
           // print_UARTx(&huart5, "%s\n", _scr_temp._s32_temp);
            print_UARTx(&huart6, "%s%d\xff\xff\xff", "main.n0.val=", _scr_temp._s32_temp);
            print_UARTx(&huart6, "%s%d\xff\xff\xff", "main5.n2.val=", _scr_temp._s32_temp);
            break;
        }
        case 1: //y
        {
            print_UARTx(&huart6, "%s%d\xff\xff\xff", "main.n1.val=", _scr_temp._s32_temp);
            print_UARTx(&huart6, "%s%d\xff\xff\xff", "main5.n3.val=", _scr_temp._s32_temp);
            break;
        }
        case 2://voltage_100
        {
            print_UARTx(&huart6, "%s%d\xff\xff\xff", "main4.x0.val=", _scr_temp._s32_temp);
            break;
        }
        case 3://temperature * 100
        {
           //print_UARTx(&huart5, "%s%d\xff\xff\xff", "main4.x1.val=", _scr_temp._s32_temp);
            print_UARTx(&huart6, "%s%d\xff\xff\xff", "main4.x1.val=", _scr_temp._s32_temp);
            break;
        }
        case 4://now
        {
            print_UARTx(&huart6, "%s%d\xff\xff\xff", "main.n2.val=", _scr_temp._s32_temp);
            break;
        }
        case 5://mission_number
        {
            print_UARTx(&huart6, "%s%d\xff\xff\xff", "main3.n0.val=", _scr_temp._s32_temp);
            break;
        }
        case 6://shoot_target
        {
            print_UARTx(&huart6, "%s%d\xff\xff\xff", "main2.n1.val=", _scr_temp._s32_temp);
            break;
        }
        case 7://camera_x_pix
        {
            print_UARTx(&huart6, "%s%d\xff\xff\xff", "main4.n1.val=", _scr_temp._s32_temp);
            break;
        }
        case 8://camera_y_pix
        {
            print_UARTx(&huart6, "%s%d\xff\xff\xff", "main4.n2.val=", _scr_temp._s32_temp);
            break;
        }
        case 9://camera_detector
        {
            print_UARTx(&huart6, "%s%d\xff\xff\xff", "main4.n0.val=", _scr_temp._s32_temp);
            break;
        }
        case 10://x_vel
        {
            print_UARTx(&huart6, "%s%d\xff\xff\xff", "main5.n5.val=", _scr_temp._s32_temp);
            break;
        }
        case 11://y_vel
        {
            print_UARTx(&huart6, "%s%d\xff\xff\xff", "main5.n6.val=", _scr_temp._s32_temp);
            break;
        }
        case 12://now_wp_x
        {
            print_UARTx(&huart6, "%s%d\xff\xff\xff", "main5.n0.val=", _scr_temp._s32_temp);
            break;
        }
        case 13://now_wp_y
        {
            print_UARTx(&huart6, "%s%d\xff\xff\xff", "main5.n1.val=", _scr_temp._s32_temp);
            break;
        }
    }
}

void Transmit_SCreenData2up(uint8_t len,uint8_t*data) //转发串口屏数据到主控
{
    HAL_UART_Transmit(&huart3, (uint8_t *)data, len, 100);
//    HAL_UART_Transmit(&huart5, (uint8_t *)data, len, 100);
//    for(int j=0;j<len;j++)
//    {
//        HAL_UART_Transmit(&huart3, (uint8_t *)data+j, 1, 100);
//    }
}
void Screen_task(void)
{

}
